xlv.obj.uploader={
	extend:function(){
	},
	implement:function(){
		this.implement('xlv.imp.mvc');
	},
	create:function(tag){
		var div=document.createElement((tag||'div'));
		div.setAttribute('xlvobject','xlv.obj.uploader');
		Element.addClassName(div,'class_uploader');
		return div;			
	},
	properties:{
		dmName:null,
		methodName:'upload', 
		valueMap:null,
		showSubmit:true,
		srcImg:'/xlive/images/_upload.png',
		srcImgWait:'/xlive/images/_loading.gif',
		promptText:'add upload files ...',
		limit:0
	},
	method:{
		construct:function(){
			Element.addClassName(this,'class_uploader');
			var v=this.$V;
			v.form=xlv.util.findChild(this,{tag:'form'});
			if(!v.form){
				var name='n'+new Date().getTime();
				var tag='<form method="post" enctype="multipart/form-data" accept-charset="UTF-8" target="';
				tag +=name+'" name="'+name+'" '+'action=""></form>';
				this.innerHTML=tag;
				v.form=xlv.util.findChild(this,{tag:'form'});
				tag='<input type="hidden" name="_charset_" id="_charset_">';
				v.form.innerHTML=tag;
			}
			v.submit=xlv.util.findChild(v.form,{id:'xlvid_uploader_submit'});
			if(!v.submit){
				v.submit=document.createElement('img');
				v.submit.setAttribute('src',this.$P.srcImg);
				v.submit.setAttribute('id', 'xlvid_uploader_submit');
				v.submit.setAttribute('title', 'upload');
				v.submit.style.display='none';
				v.form.appendChild(v.submit);
			}
			v.table=xlv.util.findChild(v.form,{tag:'table'});
			if(!v.table)v.form.appendChild(v.table=document.createElement('table'));
			v.tbody=xlv.util.findChild(v.table,{tag:'tbody'});
			if(!v.tbody)v.table.appendChild(v.tbody=document.createElement('tbody'));
			if(v.tbody.rows.length>0) v._definedRow=v.tbody.rows[0];
			else this._appendPromptRow();
			
			v.table.setAttribute('id','xlvid_uploader_table');
			v.tbody.setAttribute('id','xlvid_uploader_body');
			this.styleForId(v.submit,v.submit.id);
			this.styleForId(v.table,v.table.id);
			this.styleForId(v.tbody,v.tbody.id);
		},
		initial:function(){
			this.addEventHandler('click','_click');
		},
		styleForId:function(facilities,id){
		},
		_click:function(event){
			if(event.target.id=='xlvid_uploader_submit') this.process();
		},
		cleanUploadList:function(){
			var v=this.$V;
			for(var i=v.tbody.rows.length-2;i>=0;--i){
				var row=v.tbody.rows[i];
				var file=xlv.util.findChild(row,{id:'xlvid_uploader_file'});
				var check=xlv.util.findChild(row,{id:'xlvid_uploader_check'});
				this.removeObserve('change','_fileChanged',file);
				if(Prototype.Browser.IE) this.removeObserve('click','_checkChanged',check);
				else this.removeObserve('change','_checkChanged',check);
				this.$V.tbody.deleteRow(i);
			}
			this._onOffSubmit();
		},
		valueMap:function(){
			var dm=this.getDataModel(this.$P.dmName);
			var map=this.$P.valueMap;
			if(dm&&map){
				for(var v in map){
					var value=dm.getValue(v);
					dm.setValue(value,map[v]);
				}
			}
		},
		fileCount:function(){
			return this.$V.tbody.rows.length-1;
		},
		_appendPromptRow:function(prompt_msg){
			prompt_msg=(prompt_msg||this.$P.promptText);
			var v=this.$V;
			var row=v.tbody.insertRow(-1);
			row.insertCell(-1);
			row.insertCell(-1);
			var check=document.createElement('input');
			check.setAttribute('name', 'checkbox');
			check.setAttribute('type', 'checkbox');
			check.setAttribute('id', 'xlvid_uploader_check');
			check.style.display='none';
			row.cells[0].appendChild(check);
			var div=document.createElement('div');
			div.setAttribute('id', 'xlvid_uploader_name');
			div.style.position='relative';
			if(prompt_msg) xlv.util.setElementValue(div,prompt_msg);
			row.cells[1].appendChild(div);
			var file=document.createElement('input');
			file.setAttribute('type', 'file');
			file.setAttribute('name', 'f'+new Date().getTime());
			file.setAttribute('id', 'xlvid_uploader_file');
			Element.setStyle(file,{position:'absolute',left:'0px',top:'0px',opacity:0,cursor:'pointer'});
			if(Prototype.Browser.IE) {
				file.style.left='0px';
				file.style.marginLeft='-720px';
				file.style.borderRightWidth='600px';
				file.setAttribute('size', 1);
			}
			else file.setAttribute('size', '200');
			div.appendChild(file);
			this.addEventHandler('change','_fileChanged', file);
			if(Prototype.Browser.IE) this.addEventHandler('click','_checkChanged', check);
			else this.addEventHandler('change','_checkChanged', check);
			
			this.styleForId(check,check.id);
			this.styleForId(div,div.id);
			this.styleForId(file,file.id);
			return div;
		},
		dispose:function(){
		},
		_checkChanged:function(event){
			var check=(event.target.id=='xlvid_uploader_check')?event.target:null;
			if(check.checked==false){
				var row=this.targetElement(check,{tag:'tr'});
				var file=xlv.util.findChild(row,{id:'xlvid_uploader_file'});
				var name=xlv.util.findChild(row,{id:'xlvid_uploader_name'});
				this.removeObserve('change','_fileChanged',file);
				if(Prototype.Browser.IE) this.removeObserve('click','_checkChanged',check);
				else this.removeObserve('change','_checkChanged',check);
				this.$V.tbody.deleteRow(row.rowIndex);
				this._onOffSubmit();
			}
		},
		_fileChanged:function(event){
			var row=this.targetElement(event.target,{tag:'tr'});
			var check=xlv.util.findChild(row,{id:'xlvid_uploader_check'});
			var name=xlv.util.findChild(row,{id:'xlvid_uploader_name'});
			var file=xlv.util.findChild(row,{id:'xlvid_uploader_file'});
			if(file.value.strip()==''){
				if(row.rowIndex==0&&this.$V.tbody.rows.length==1)return;
				this.removeObserve('change','_fileChanged',file);
				if(Prototype.Browser.IE) this.removeObserve('click','_checkChanged',check);
				else this.removeObserve('change','_checkChanged',check);
				this.$V.tbody.deleteRow(row.rowIndex);
				this._onOffSubmit();
				return;
			}
			xlv.util.setElementValue(name,file.value.replace(/.*(\/|\\)/,''));
			if(row.rowIndex != this.$V.tbody.rows.length-1)return;
			check.checked=true;
			check.style.display='';
			this._appendPromptRow();
			this._onOffSubmit();
		},
		_onOffSubmit:function(){
			var v=this.$V;
			if(this.$P.showSubmit)v.submit.style.display=(v.tbody.rows.length>1)?'':'none';
			if(this.$P.limit>0){
				var row=v.tbody.rows[v.tbody.rows.length-1];
				if(row){
					var file=xlv.util.findChild(row,{id:'xlvid_uploader_file'});
					var name=xlv.util.findChild(row,{id:'xlvid_uploader_name'});
					if(file){
						if(this.fileCount() >= this.$P.limit) {
							file.disabled=true;
							name.style.display='none';
						}else {
							file.disabled=false;
							name.style.display='';
						}
					}
				}
			}
		},
		_makeActionString:function(){
			var qstr=null;
			if(this.$P.dmName){
				var dm=this.getDataModel(this.$P.dmName);
				if(dm){
					var action = dm.getValue('properties/upload/gae-action');
					var node=dm.getNode(this.$P.methodName);
					if(node){
						qstr=xlv.util.methodNodeToQueryString(node);
						if(action && action != '') qstr=action;
						else qstr=dm.$P._servlet+'/'+dm.$P.objectURL.replace(/\./g,'/')+'?'+qstr;
					}
				}
			}
			return qstr;
		},
		uploadResposed:function(valid,why){
		},
		mvc_modelChanged:function(dm,notify,memo){
			var gae = dm.getValue('properties/upload/gae-blobstore');
			if("true"==gae){
				if('setChanged'==notify&&memo.methods=='gae-upload-url') {
					this._process();
					return;
				}
			}
			this.$super('xlv.imp.mvc.mvc_modelChanged',dm,notify,memo);
		},
		process:function(){
			this._process();
		},
		_process:function(){
			if(this.fileCount()==0){
				this.uploadResposed(true,'no file upload');
				return;
			}
			var v=this.$V;
			this.valueMap();
			var dm=this.getDataModel(this.$P.dmName);
			var gae = dm.getValue('properties/upload/gae-blobstore');
			if("true"==gae){
				var url=dm.getValue('properties/gae-upload-url/return/url');
				if(!url||url==''){
					dm.setValue(dm.getValue('properties/upload/work-directory'),'properties/gae-upload-url/work-directory');
					dm.setValue(dm.getValue('properties/upload/prefix-file-name'),'properties/gae-upload-url/prefix-file-name');
					dm.webMethod('gae-upload-url');
					return;
				}
				dm.setValue(url,'properties/upload/gae-action');
				dm.setValue('','properties/gae-upload-url/return/url');
			}
			var action=this._makeActionString();
			if(!action) return;
			v.form.setAttribute('action',action);
			//
			var name=v.form.getAttribute('name');
			var div=document.createElement('div');
			div.innerHTML='<iframe name="'+name+'" id="'+name+'" src="javascript:void(0);"></iframe>';
			v.iframe=xlv.util.findChild(div,{tag:'iframe'});
			v.iframe.style.visibility='hidden';
			v.iframe.style.height='1px';
			v.iframe.style.width='1px';
			document.body.appendChild(v.iframe);
			this.addEventHandler('load','_iframeLoad', v.iframe);
			v.remove_frame=false;
			//
			v.glass=document.createElement('div');
			var style={position:'absolute',display:'block',left:'0px',top:'0px',bottom:'0px',right:'0px',opacity:'0.1',background:'blue'};
			Element.setStyle(v.glass,style);
			this.appendChild(v.glass);
			v.wait=document.createElement('img');
			v.wait.setAttribute("src", this.$P.srcImgWait);
			Element.setStyle(v.wait,{position:'absolute',left:'50%',top:'10%'});
			this.appendChild(v.wait);
			//
			v.form.submit();
		},
		_iframeLoad:function(event){
			var v=this.$V;
			if(v.iframe.src=="about:blank"){						
				if(v.remove_frame){
					this.removeChild(v.wait);
					this.removeChild(v.glass);
					this.removeObserve('load','_iframeLoad', v.iframe);
					setTimeout(function(){v.iframe.parentNode.removeChild(v.iframe);v.iframe=null}, 1);
				}
				return;
			}
			var valid=true,why='???';
			try{
				var doc = v.iframe.contentDocument ? v.iframe.contentDocument : window.parent.frames[v.iframe.id].document;
				if(doc.readyState && doc.readyState != 'complete')return; //	fixing Opera 9.26
				var dm=this.getDataModel(this.$P.dmName);
				dm.$V.lastMethodName='upload';
				if(!Prototype.Browser.IE && doc.documentElement){
					dm.xml_properties.responseXML=doc;
				}else{
					var text=doc.documentElement.innerText;
					var return_nodes=dm.xml_createElements('xlive.upload.return');
					var last=return_nodes[return_nodes.length-1];
					var match=text.match(/<data>([\S\s]*?)<\/data>/img);
					if(match!=null)	last.appendChild(dm.xml_stringToNode(match[0]));
					match=text.match(/<valid>([\S\s]*?)<\/valid>/img);
					if(match!=null)	last.appendChild(dm.xml_stringToNode(match[0]));
					match=text.match(/<why>([\S\s]*?)<\/why>/img);
					if(match!=null)	last.appendChild(dm.xml_stringToNode(match[0]));
					dm.xml_properties.responseXML= new ActiveXObject('Msxml2.DOMDocument');
					dm.xml_properties.responseXML.setProperty('SelectionLanguage','XPath');
					dm.xml_properties.responseXML.appendChild(return_nodes[0]);
				}
				dm.webObjectResponse(true);
				var rows=v.tbody.rows;
				for(var i=0;i<rows.length-1;++i){
					var file=xlv.util.findChild(rows[i].cells[1],{id:'xlvid_uploader_file'});
					var name=xlv.util.findChild(rows[i].cells[1],{id:'xlvid_uploader_name'});
					var length=dm.getValue('properties/upload/return/data/file[@inputName="'+file.getAttribute('name')+'"]/@length');
					xlv.util.setElementValue(name,file.value.replace(/.*(\/|\\)/,'')+' -- '+length+' bytes');
				}
				valid= ('false'== dm.getValue('properties/upload/return/valid'))?false:true;
				why=dm.getValue('properties/upload/return/why');
			}catch(e){alert(e);valid=false;why=e.message;}
			this.uploadResposed(valid,why);
			v.remove_frame = true;				
			v.iframe.src="about:blank";							
		}
	}
};
